package com.example.testlxr9096.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.testlxr9096.R;
import com.example.testlxr9096.bean.IoItem;

import java.util.List;

public  class DetectItemAdapter extends RecyclerView.Adapter<DetectItemAdapter.ViewHolder> {

    private final List<IoItem> ioItems;
    private OnItemClickListener listener;



    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public DetectItemAdapter(List<IoItem> ioItems) {
        this.ioItems = ioItems;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_detect, parent, false);
        return new ViewHolder(view);
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        IoItem item = ioItems.get(position);
        holder.ioName.setText(item.getName());

//        if (item.isSelected()) {
//            holder.   container.setBackgroundResource(R.drawable.item_border_selected);
//        } else {
//            holder.   container.setBackgroundResource(R.drawable.item_border_normal);
//        }



        if (item.getState()==0){
            holder.ioName.setBackgroundResource(R.color.grey);

        }
        else if (item.getState()==1){
            holder.ioName.setBackgroundResource(R.color.qualified);

        }else {
            holder.ioName.setBackgroundResource(R.color.alerm);

        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = holder.getAdapterPosition();
                if (pos != RecyclerView.NO_POSITION) {
                    if (listener != null) {
                        listener.onItemClick(position);
                    }
                }

            }
        });

    }

    @Override
    public int getItemCount() {
        return ioItems.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        public TextView ioName;
        private LinearLayout container,ll_1;



        public ViewHolder(View itemView) {
            super(itemView);
            ioName = itemView.findViewById(R.id.io_name);
            container = itemView.findViewById(R.id.frm_1);
            ll_1 = itemView.findViewById(R.id.ll_view);}}



}